package ar.edu.unicen.exa.isistan.archassis.view.toolbar;


import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import swing2swt.layout.BorderLayout;
import ar.edu.unicen.exa.isistan.archassis.ia.api.EvalResult;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.ListCases;
import ar.edu.unicen.exa.isistan.archassis.model.ModelManager;
import ar.edu.unicen.exa.isistan.archassis.model.listeners.IClassesListener;
import ar.edu.unicen.exa.isistan.archassis.model.listeners.ISelectedCaseListener;
import ar.edu.unicen.exa.isistan.archassis.view.Application;
import ar.edu.unicen.exa.isistan.archassis.view.LongOperations;
import ar.edu.unicen.exa.isistan.archassis.view.dialogs.AddCaseDialog;
import ar.edu.unicen.exa.isistan.archassis.view.dialogs.SelectCaseDialog;
import ar.edu.unicen.exa.isistan.archassis.view.rcs.SVNComposite;

/**
 * Esta clase encapsula el funcionamiento de la ToolBar de la manera mas simple
 * que encontramos, teniendo en cuenta que {@link ToolBar} "is <i>not</i> 
 * intended to be subclassed".
 * 
 * Cuidado, asumo que el composite padre tiene el layout {@link swing2swt.layout.BorderLayout}
 * 
 * @author sebastianperruolo@gmail.com
 * @author sackiti@gmail.com
 */
public class ToolBarUtil implements IClassesListener, ISelectedCaseListener {
	private ToolItem addCaseListToolItem;
	private ToolItem addCaseItemToolItem;
	private Shell shell;
	//private ToolItem tltmAgregarCaso;
//	public ToolBarUtil(final Composite parentComposite) {
//		new ToolBarComposite(parentComposite);
//	}
	
	public ToolBarUtil(final Composite parentComposite) {
		shell = parentComposite.getShell();
		ToolBar toolBar = new ToolBar(parentComposite, SWT.FLAT | SWT.RIGHT);
		toolBar.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		//asumo que el composite padre tien el layout {@link swing2swt.layout.BorderLayout}
		toolBar.setLayoutData(BorderLayout.NORTH);
		
		ToolItem tltmCambiarRepositorio = new ToolItem(toolBar, SWT.NONE);
		tltmCambiarRepositorio.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SVNComposite.openDialog(parentComposite.getShell());
			}
		});
		tltmCambiarRepositorio.setImage(SWTResourceManager.getImage(Application.class, "/img/tb/world.png"));
		tltmCambiarRepositorio.setText("Repositorio");
		
		addCaseListToolItem = new ToolItem(toolBar, SWT.NONE);
		addCaseListToolItem.setDisabledImage(SWTResourceManager.getImage(Application.class, "/img/tb/add_g.png"));
		addCaseListToolItem.setImage(SWTResourceManager.getImage(Application.class, "/img/tb/add_c.png"));
		addCaseListToolItem.setText("Agregar caso nuevo");
		addCaseListToolItem.setEnabled(false);
		addCaseListToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				openNewCaseDialog(false);
			}
		});
		
		addCaseItemToolItem = new ToolItem(toolBar, SWT.NONE);
		addCaseItemToolItem.setDisabledImage(SWTResourceManager.getImage(Application.class, "/img/tb/add_g.png"));
		addCaseItemToolItem.setImage(SWTResourceManager.getImage(Application.class, "/img/tb/add_c.png"));
		addCaseItemToolItem.setText("Agregar ejemplo al caso");
		addCaseItemToolItem.setEnabled(false);
		addCaseItemToolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				openNewCaseDialog(true);
			}
		});
		
		ToolItem tltmSalir = new ToolItem(toolBar, SWT.NONE);
		tltmSalir.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				parentComposite.getShell().dispose();
			}
		});
		tltmSalir.setImage(SWTResourceManager.getImage(Application.class, "/img/tb/power.png"));
		tltmSalir.setText("Salir");
		
		ModelManager.getInstance().addClassesListener(this);
		ModelManager.getInstance().addSelectedCaseListener(this);
	}
	
	protected void openNewCaseDialog(boolean addToACase) {
		AddCaseDialog addCaseDialog = new AddCaseDialog(shell);
		addCaseDialog.setClassesList(classesList);
		
		if (addToACase) {
			SelectCaseDialog selectCaseDialog = new SelectCaseDialog(shell);
			ListCases listCase = selectCaseDialog.open();
			
			if (listCase == null) {
				return;
			}
			
			addCaseDialog.setListCases(listCase);
		}
		

		Object r = addCaseDialog.open();
		if (r == null) {
			//no hizo nada
		} else {
			//agrego el caso, refrescar
			List<JavaClass> r2 = LongOperations.getJavaClassList(shell);
			ModelManager.getInstance().setClassesList(r2);
		}
		
	}

	private List<JavaClass> classesList = null;
	@Override
	public void change(List<JavaClass> classesList) {
		this.classesList = classesList;
		addCaseListToolItem.setEnabled( classesList != null );
	}

	private EvalResult evalResult = null;
	@Override
	public void change(EvalResult evalResult) {
		this.evalResult = evalResult;
		addCaseItemToolItem.setEnabled( evalResult != null );
	}

	
}
